#include "CGUIGame.h"

#include "CMinionNode.h"
#include "CStateGame.h"
#include "COverlord.h"

CGUIGame::CGUIGame(CStateGame * state): state(state)
{
  env = state->getManager()->getGUIEnv();
}

CGUIGame::~CGUIGame()
{
  parent->remove();
}

void CGUIGame::init()
{
  // Control variables for ui (left, right, up, down, temp var a )

  short lf, rt, up, dn, a;

  parent = env->addTab (rect<s32>(0, 0, 800, 600));

  IGUIImage * bar= env->addImage( rect<s32>(600, 0, 800, 600), parent);
  bar->setImage(state->getDriver()->getTexture("sidebar.png"));
  bar->setScaleImage(true);
  bar->setUseAlphaChannel(true);

  //! Command Buttons
  env->addButton(rect<s32>(620, 435, 780, 460), parent, BTN_ATTACK, L"Attack");
  env->addButton(rect<s32>(620, 475, 780, 500), parent, BTN_MOVE, L"Move");
  env->addButton(rect<s32>(620, 515, 780, 540), parent, BTN_CANCEL, L"Cancel");
  env->addButton(rect<s32>(620, 555, 780, 580), parent, BTN_POPSTATE, L"Leave Game");

  lf = 10;
  rt = lf + 40;

  dn = SCREEN_H - 10;
  up = dn - 40;

  //! Disabled. Overlord spells does not have network support
  //env->addButton(rect<s32>(lf, up, rt, dn), parent, BTN_SPELL, L"Heal");

  lf += 200;
  rt += 200;

  env->addButton(rect<s32>(lf, up, rt, dn), parent, BTN_ABILITY_1, L"Bolt");

  lf += 50;
  rt += 50;
  env->addButton(rect<s32>(lf, up, rt, dn), parent, BTN_ABILITY_2, L"Heal");

  lf += 50;
  rt += 50;
  env->addButton(rect<s32>(lf, up, rt, dn), parent, BTN_ABILITY_3, L"Blast");

  //! Selected Minion Details

  // Preset background
  a = 20; // This means text height

  rt = SCREEN_W - 20;
  lf = rt - 160;

  dn = 420;
  up = dn - ( ( a * 8 ) + 10 );
  dispDelayDown = up;

  // "background" for minion info UI
  IGUIStaticText * bg;
  bg = env->addStaticText(L"", rect<s32>(lf, up, rt, dn), false, true, parent, -1, true);
  bg->setDrawBorder(true);

  lf += 5;
  rt -= 5;
  up += 5;
  dn = up + a;

  // Name
  txtName   = env->addStaticText(L"No Minion Selected", rect<s32>(lf, up, rt, dn), false, true, parent );

  // Level
  up += a; dn += a;
  txtLvl    = env->addStaticText(L"Level: ", rect<s32>(lf, up, rt, dn), false, true, parent);

  // Attack
  up += a; dn += a;
  txtAtk    = env->addStaticText(L"Attack: ", rect<s32>(lf, up, rt, dn), false, true, parent);

  // Defend
  up += a; dn += a;
  txtDef    = env->addStaticText(L"Defend: ", rect<s32>(lf, up, rt, dn), false, true, parent);

  // Magic Attack
  up += a; dn += a;
  txtMAtk   = env->addStaticText(L"Magic: ", rect<s32>(lf, up, rt, dn), false, true, parent);

  // Magic Defend
  up += a; dn += a;
  txtMDef   = env->addStaticText(L"M Def: ", rect<s32>(lf, up, rt, dn), false, true, parent);

  // Hit points (HP)
  up += a; dn += a;
  txtHP     = env->addStaticText(L"Hp: ", rect<s32>(lf, up, rt, dn), false, true, parent);

  // Magic points (MP)
  up += a; dn += a;
  txtMP     = env->addStaticText(L"Mp: ", rect<s32>(lf, up, rt, dn), false, true, parent);

  /// Delay Display
  // Preset background
  a = 20; // This means text height

  dn = dispDelayDown;
  up = dn - ( a * 6 );

  // "background" for delay display
  dispDelay = env->addStaticText(L"", rect<s32>(lf, up, rt, dn), false, true, parent);
  dispDelay->setDrawBorder(true);

}

void CGUIGame::update()
{

}

void CGUIGame::setVisible(bool visible)
{
  // Show the GUI elements
  if ( visible )
    parent->setVisible(true);
  // Hide the GUI elements
  else
    parent->setVisible(false);
}

bool CGUIGame::onGUIEvent(const SEvent& event)
{
  return true;
}

void CGUIGame::updateMinion()
{
  CMinionNode * selected = state->getSelectedMinion();
  CMinionNode * pointed = state->getPointedMinion();
  if ( pointed )
  {
    selected = pointed;
  }

  if ( selected )
  {
    stringw tmp;
    txtName->setText(selected->getStats().name.c_str());
    txtName->setOverrideColor(selected->getOwner()->getTeamColor());

    tmp = L"Level: ";
    tmp += selected->getStats().level;
    txtLvl->setText(tmp.c_str());

    tmp = L"Attack: ";
    tmp += selected->getStats().atk;
    txtAtk->setText(tmp.c_str());

    tmp = L"Defend: ";
    tmp += selected->getStats().def;
    txtDef->setText(tmp.c_str());

    tmp = L"Magic: ";
    tmp += selected->getStats().mAtk;
    txtMAtk->setText(tmp.c_str());

    tmp = L"M Def: ";
    tmp += selected->getStats().mDef;
    txtMDef->setText(tmp.c_str());

    tmp = L"Hp: ";
    tmp += selected->getStats().hp;
    tmp += L" / ";
    tmp += selected->getStats().maxHp;
    txtHP->setText(tmp.c_str());

    tmp = L"Mp: ";
    tmp += selected->getStats().mp;
    tmp += L" / ";
    tmp += selected->getStats().maxMp;
    txtMP->setText(tmp.c_str());
  }
  else
  {
    txtName->setText(L"No Minion Selected");
    txtLvl->setText(L"Level: ");
    txtAtk->setText(L"Attack: ");
    txtDef->setText(L"Defend: ");
    txtMAtk->setText(L"Magic: ");
    txtMDef->setText(L"M Def: ");
    txtHP->setText(L"Hp: ");
    txtMP->setText(L"Mp: ");
  }
}

void CGUIGame::updateDelay(vector <CMinionNode*> &minionList)
{
  /// Delay display

  // Start by drawing background tab
  int lf = SCREEN_W - 180;
  int rt = SCREEN_W - 20;
  int up = dispDelayDown - ( 20 * minionList.size() + 10 ) ;;
  int dn = dispDelayDown;
  dispDelay->setRelativePosition( rect<s32>(lf, up, rt, dn) );


  // Then draw the list
  rt = rt - lf - 5;
  lf =  5;
  up =  5;
  dn = up + 20;

  // Synchronize the list with
  while ( minionList.size() != listDelay.size() )
  {
    if ( minionList.size() > listDelay.size() )
    {
      IGUIStaticText * txtNew = env->addStaticText(L"mmmm", rect<s32>(lf, up, rt, dn), false, true, dispDelay);
      listDelay.push_back(txtNew);
      up += 20;
      dn += 20;
    }

    else if (  minionList.size() < listDelay.size() )
    {
      listDelay.back()->remove();
      listDelay.pop_back();
    }
  }


  for ( unsigned int i=0; i<minionList.size(); i++ )
  {
    CMinionNode *pMinion = minionList[i];

    stringw tmp = pMinion->getStats().name;
    tmp += "  ";
    tmp += pMinion->getDelay();

    IGUIStaticText* pText = listDelay[i];
    pText->setText(tmp.c_str());
    pText->setOverrideColor(pMinion->getOwner()->getTeamColor());


  }

  state->nextMinion();
}
